local app = require "app"
local game = require "club.threecard.hbsl"

local GameObjectExtension = CS.Blaze.GameObjectExtension
local Vector3 = CS.UnityEngine.Vector3

local M = game.ui.element()

function M:ctor()
    local AvatarIcon = require "ui.Common.AvatarIcon"
    self.avatar = AvatarIcon.new()
end

function M:pool_get(pool)
    self:show()
end

function M:pool_put(pool)
    self:hide()
end

function M.pool_new(pool)
    local options = pool.options
    local go = GameObjectExtension.GetFromPool(options.prefab)
    local ret = M.new()
    ret:set_gameobject(go, true)
    ret:set_parent(options.parent)
    ret:load()
    return ret
end

function M:on_set_gameobject()
    -- self.canvas.enabled = self.visible
end

function M:on_set_visibility()
    self.canvas.enabled = self.visible
end

function M:set_player(player)
    self.player = player
    self.avatar:set_player(player)
    self.name_txt.text = player.name
    self.flag_self:SetActive(player.id == game.kbe.player().id)
    self.flag_banker:SetActive(player.id == game.store.gameplay.state.last_sender_id)
end

function M:appear(from_x, from_y, offset)
    self.result_txt.text = "未公布"
    self.result_tip:SetActive(false)
    self.payout_tip:SetActive(false)
    self.flag_crown:SetActive(false)
    self.icon_1:SetActive(true)
    self.icon_2:SetActive(false)
    self.bomb_obj:SetActive(false)
    self.transform.localPosition = Vector3(from_x, from_y, 0)
    local to = Vector3(from_x + offset, from_y, 0)
    self.transform:DOLocalMove(to, 0.3)
end

function M:on_injected(bind)
    bind("gameobject.element", self.avatar_obj, self.avatar)
end

function M:on_reveal(amount, is_hit, max_amount)
    if is_hit then
        self.result_txt.text = string.format("%s", amount)
    else
        self.result_txt.text = amount
        self.bomb_obj:SetActive(false)
    end

    self.result_tip:SetActive(true)
    self.payout_tip:SetActive(false)
    self.result_tip_txt.text = "+".. app.i18n.number(amount, "chip4")
    self.icon_1:SetActive(not is_hit)
    self.icon_2:SetActive(is_hit)
    self.flag_crown:SetActive(amount == max_amount)
end

function M:on_settle(payout)
    self.bomb_obj:SetActive(true)
    game.audio.post("hbsl:bomb")
    self.result_tip:SetActive(false)
    self.payout_tip:SetActive(true)
    self.payout_tip_txt.text = "-" .. app.i18n.number(payout, "chip4")
end

function M:on_release()
    self:super("on_release")
    self.avatar:release()
end

return M